﻿/*
* 作者:haojielyb
* 邮箱:haojielyb@qq.com
*/

using Furion.DependencyInjection;
using Furion.Logging.Extensions;
using Lxb.Model;

using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace rfid9500_Mqtt

{
    public class OthUserService : ITransient
    {
        private readonly ISugarQueryable<Users> repository; // 仓储对象：封装简单的CRUD
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        private readonly string[] schools;
        private string path = AppDomain.CurrentDomain.BaseDirectory;

        public OthUserService(ISqlSugarClient db)
        {
          
            this.db = db.CopyNew();    // 推荐操作
            repository = this.db.Queryable<Users>();
         
            schools = new[] { "迁西县第二中学" };
        }

        public Users Get(int user_id)
        {
            try
            {
                var u = repository.Where(u => u.user_id == user_id).First();
                return u;
            }
            catch (Exception e)
            {

                throw e;
            }
            
           
        }

       


        private String dellasttime = "";

        public List<Users_del> GetDel(String devid, int size = 50, int page = 0)
        {
            var dev_ = db.Queryable<Device>().Where(dev => dev.SN == devid).First();

            if (dev_ == null)
            {
                return null;
            }

            var school = dev_.school;

            var datestr = "2018/1/1";

            var datetime = Convert.ToDateTime(datestr);
            try
            {
                datestr = System.IO.File.ReadAllText($"{path}dev/{devid}_d.cfg");
                datetime = Convert.ToDateTime(datestr);
            }
            catch (Exception)
            {
                datestr = "2018/1/1";
            }

            var lst = db.Queryable<Users_del>().Where(d => d.deltime > datetime && d.School == school).OrderBy(d => d.deltime, OrderByType.Asc).Skip(page * size).Take(size).ToList();
            if (lst.Count > 0)
            {
                dellasttime = lst.Last().deltime.ToString("yyyy/MM/dd HH:mm:ss");
            }
            if (lst.Count < size && (!String.IsNullOrEmpty(dellasttime)))
            {
                try
                {
                    if (!Directory.Exists($"{path}dev/"))
                    {
                        Directory.CreateDirectory($"{path}dev/");
                    }
                    System.IO.File.WriteAllText($"{path}dev/{devid}_d.cfg", dellasttime);

                }
                catch (Exception ex)
                {

                    ex.Message.LogInformation();
                }

            }
            return lst;
        }

        public  Users getUserByrfid(String rfid)
        {
            return db.Queryable<Users>().Where(u=>u.rfid.Equals(rfid)).First();
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="SN"> 设备SN</param>
        /// <param name="id">id</param>
        /// <param name="status">状态</param>
        /// <param name="why">原因</param>
        /// <returns></returns>

        internal bool Upstate(String SN, String ids, int status = 1, string why = "")
        {


            var sql = "";
            if (status == 1)
            {
                sql = $"update  lxb_face_down_log  set status =1 where SN='{SN}' and  id in ({ids})";
            }
            else
            {
                sql = $"update  lxb_face_down_log  set status =0 ,why='{why}',times=times+1  where SN='{SN}' and  id in ( {ids})";
            }

            //    sql.LogWarning();

            db.Ado.ExecuteCommandAsync(sql);
            return true;

        }
    }
}